{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的基本形式\n",
    "def greet():\n",
    "    return \"Hello, Jinzd!\"\n",
    "\n",
    "print(greet())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 带参数的函数\n",
    "def greet(firstName, lastName):\n",
    "    return f\"HI, {firstName} {lastName}\"\n",
    "\n",
    "print(greet(\"zd\", \"jin\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义加法函数\n",
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "print(add(13, 17))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数参数的缺省值\n",
    "\n",
    "def add(a = 1, b = 3):\n",
    "    return a + b\n",
    "\n",
    "print(add())\n",
    "print(add(5, 7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xargs \n",
    "def multiply(*numbers):\n",
    "    total = 1\n",
    "    \n",
    "    for num in numbers:\n",
    "        total *= num\n",
    "        \n",
    "    return total\n",
    "\n",
    "print(multiply(1, 3, 5, 7))\n",
    "\n",
    "n = [1, 2, 3, 4]\n",
    "print(multiply(*n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# xxargs\n",
    "def saveUser(**usr):\n",
    "    return usr\n",
    "\n",
    "u = saveUser(firstName = \"zd\", lastName = \"jinz\")\n",
    "print(u)\n",
    "print(u[\"firstName\"])\n",
    "print(u[\"lastName\"])\n",
    "\n",
    "u1 = {\n",
    "    \"firstName\": \"XianJun\",\n",
    "    \"lastName\": \"Huang\"\n",
    "}\n",
    "\n",
    "u2 = saveUser(**u1)\n",
    "print(u2)\n",
    "print(u2[\"firstName\"])\n",
    "print(u2[\"lastName\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [1, 2, 3, 4, 5]\n",
    "print(lst)\n",
    "print(len(lst))\n",
    "lst.append(\"abc\")\n",
    "print(lst)\n",
    "print(lst.count(2)) # 统计列表中值为2的个数\n",
    "lst2 = lst.copy()\n",
    "print(lst2)\n",
    "lst2.index(3)   # 返回元素的index"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ee5cc6fef2d70a7e71ee3826687cbd150f18158e0b1eef11d4f4f92bb920e304"
  },
  "kernelspec": {
   "display_name": "Python 3.9.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
